package project_package;

public class RentHistory {

    int numberOne = 4096; // the number of Rent we can hold
    int numberTwo = 8; // the number of fields we can have 
    int numberTotal = 0; // the total number of history
    String history[][] = new String[numberOne][numberTwo]; // our database
    private static RentHistory instance = null;
   
    public static RentHistory getInstance()
    {
    	if(instance == null)
    	{
    		instance = new RentHistory();
    	}
    	return instance;
    }
    
    // add a rent to the history so we can log the info about a rent
    public void addRentHistory(int rentID, String rent, String customer, String employee, double price, long leaseDate, long leasePeriod, long leaseReturned ) 
    {
        history[numberTotal][0] = Integer.toString(rentID);         	// 	Rent ID
        history[numberTotal][1] = rent;                             	// 	Rent
        history[numberTotal][2] = customer;                         	// 	Customer
        history[numberTotal][3] = employee;                         	// 	Employee
        history[numberTotal][4] = Double.toString(price);          	 	//	Price
        history[numberTotal][5] = String.valueOf(leaseDate);      		//	Lease Date - Unix timestamp format
        history[numberTotal][6] = String.valueOf(leasePeriod);   		// 	Lease Period - In days
        history[numberTotal][7] = String.valueOf(leaseReturned); 	 	// 	Lease Returned - Unix timtestamp format
        numberTotal++;
    }
    
    // removes a history entry
    public void removeRentHistory(int id)
    {
    	boolean found = false;
        for(int i = 0; i < numberTotal; i++) 
        {
            if (i == id) 
            {
                history[id] = null;
                System.out.println("Deleted Rent #"+id+" from Rent History");
                found = true;
            }
        }
        
        if (!found) 
        	System.out.println("Rent with ID #"+id+" was not found");
    }
    
    // gets a rent's price
    public double getRentPrice(int id)
    {
        double rentPrice = 0.0;
        for(int i = 0; i < numberTotal; i++)
        {
            if (i == id) 
            {
                rentPrice = Double.valueOf(history[i][4]);
            }
        }
        return rentPrice;
    }
    
    // gets total rent price - all the money which we have collected
    public double getTotalRentPrice()
    {
        double rentPrice = 0.0;
        for(int i = 0; i < numberTotal; i++) 
        {
            double tempPrice = Double.valueOf(history[i][4]);
            rentPrice += tempPrice;
        }
        return rentPrice;
    }
    
    // gets total rent number
    public int getTotalRentNumber()
    {
        return numberTotal;
    }
    
    
    public String[][] getHistory() {
    	return history;
    }
    // prints all rents
    public void printAllRents() 
    {
    	if (numberTotal == 0) 
    	{
    		System.out.println("No rents in the database");
    	} 
    	else {
    		System.out.println("Rent ID - Rent - Customer - Employee - Price - Lease Date - Lease Days - Lease Returned");
	        for(int i = 0; i < numberTotal; i++)
	        {
	            if (history[i] != null)
	            { 
	                for(int k = 0; k < 8; k++)
	                {
	                    System.out.print(history[i][k] + (k==7 ? "" : " - ")); // printing all the items
	                }															// (k==7 ? "" : " - ")
	                System.out.println();										// is used to whether put a dash
	            }																// or not after the 7 item
	        }
    	}
    }
}